/**
 * 微信支付
 */
let wxPay = function(openid, body, attach, out_trade_no, total_fee, goods_tag, notify_url ,successUrl){
	//统一下单
	uni.request({
		url: 'https://www.jyhzn.top/applicationTest/WxpayAPI_php/php_sdk_v3.0.10/example/jsapi.php',
		data: {
			openId: openid,
			body: body,
			attach: attach,
			out_trade_no: out_trade_no,
			total_fee: total_fee,
			goods_tag: goods_tag,
			notify_url: notify_url
		},
		method: 'POST',
		header: { 'content-type': 'application/x-www-form-urlencoded' },
		success(res) {
			console.log("统一下单: "+JSON.stringify(res));
			return;
			// 微信支付
			uni.requestPayment({
				timeStamp: res.data.timeStamp,
				nonceStr: res.data.nonceStr,
				package: res.data.package,
				signType: res.data.signType,
				paySign: res.data.paySign,
				success: (res) => {
					uni.showToast({
						title: "支付成功",
						duration: 2000						
					})
					setTimeout(function () {
						uni.switchTab({
							url: successUrl
						});								
					}, 2000)
				},
				fail: (res) => {
					uni.showModal({
						content: "支付失败: " + res.errMsg,
						showCancel: false,
					});
				},
				complete: () => {
				// this.loading = false;
				}
			})			   
		},
		fail: (data, code) => {
			uni.showToast({title: '连接失败...',icon: 'none'});
		}
	})						



}
let wxPay1 = function(body, attach, out_trade_no, total_fee, goods_tag, notify_url ,successUrl){
	uni.login({
		provider: 'weixin',
		success: function (loginRes) {
			//根据code从服务器换取openid
			var params = {
				'code':loginRes.code,
				'act': 'liBaJiangRenWXloginCode'
			};	
			var data='data='+JSON.stringify(params);
			uni.request({
				url: 'https://www.jyhzn.top/applicationTest/liBaJiangRen/serverResponse/WXminiAPI.php',
				data: data,
				method : "POST",
				header : {'content-type':'application/x-www-form-urlencoded'},					
				success: data => {
					if (data.statusCode == 200) {
						console.log(data.data.openid);
						console.log(body);
						console.log(attach);
						console.log(out_trade_no);
						console.log(total_fee);
						console.log(goods_tag);
						console.log(notify_url);
						//统一下单
						uni.request({
							url: 'https://www.jyhzn.top/applicationTest/WxpayAPI_php/php_sdk_v3.0.10/myFunction/myJsapi.php',
							data: {
								openId: data.data.openid,
								body: body,
								attach: attach,
								out_trade_no: out_trade_no,
								total_fee: total_fee,
								goods_tag: goods_tag,
								notify_url: notify_url
							},
							method: 'POST',
							header: { 'content-type': 'application/x-www-form-urlencoded' },
							success(res) {
								console.log(res);
								// 微信支付
								uni.requestPayment({
									timeStamp: res.data.timeStamp,
									nonceStr: res.data.nonceStr,
									package: res.data.package,
									signType: res.data.signType,
									paySign: res.data.paySign,
									success: (res) => {
										uni.showToast({
											title: "支付成功",
											duration: 2000						
										})
										setTimeout(function () {
											uni.switchTab({
												url: successUrl
											});								
										}, 2000)
									},
									fail: (res) => {
										uni.showModal({
											content: "支付失败: " + res.errMsg,
											showCancel: false,
										});
									},
									complete: () => {
									// this.loading = false;
									}
								})			   
							},
							fail: (data, code) => {
								uni.showToast({title: '连接失败...',icon: 'none'});
							}
						})						
					}else{
						uni.showToast({title: '服务器返回失败...',icon: 'none'});
						return false;								
					}
				},
				fail: (data, code) => {
					uni.showToast({title: '连接失败...',icon: 'none'});
					return false;		
				}					
			});
		},
		fail: function(e){
			uni.showToast({title: '微信授权登录失败',icon: 'none'});
			return false;						  
		}
	});	
}


export default {
	wxPay,
	wxPay1
};
